package com.woniuxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.dto.MenusDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsSmallDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.PageGoodsGeneralDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsGeneralDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.UpdateGoodsGeneralDto;
import com.woniuxy.entity.GoodsBrandGeneral;
import com.woniuxy.entity.GoodsGeneral;
import com.woniuxy.entity.MenuTable;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.GoodsGeneralException;
import com.woniuxy.mapper.GoodsGeneralMapper;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.AddGoodsGeneralParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.DeleteGeneralParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.ListGoodsGeneralParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.UpdateGoodsGeneralParam;
import com.woniuxy.service.GoodsBrandGeneralService;
import com.woniuxy.service.GoodsGeneralService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.util.WoniuBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 范腾龙
 * @since 2022-1-1
 * 商品大类信息管理服务实现类
 */
@Service
public class GoodsGeneralServiceImpl extends ServiceImpl<GoodsGeneralMapper, GoodsGeneral> implements GoodsGeneralService {


    @Resource
    private GoodsBrandGeneralService service;

    /**
     * 新增大类信息
     *
     * @param addGoodsGeneralParam 前端发送的新增大类的信息
     * @return 返回给前端的大类信息
     */
    @Override
    public GoodsGeneralDto saveGeneralInfo(AddGoodsGeneralParam addGoodsGeneralParam) {

        GoodsGeneral generalByName = getOne(new QueryWrapper<GoodsGeneral>().eq("goods_general_name", addGoodsGeneralParam.getGoodsGeneralName()));
        if (null != generalByName) throw new GoodsGeneralException("大类信息已存在，新增失败", 1050);
        String goodsGeneralAppraisal = addGoodsGeneralParam.getGoodsGeneralAppraisal();
        if (goodsGeneralAppraisal == null || goodsGeneralAppraisal.equals(""))
            throw new GoodsGeneralException("大类不能没有自定义图鉴", 1050);
        //满足新增条件 开始新增
        GoodsGeneral goodsGeneral = new GoodsGeneral();
        BeanUtils.copyProperties(addGoodsGeneralParam, goodsGeneral);
        if (!save(goodsGeneral)) throw new GoodsGeneralException("大类新增失败", 1055);
        GoodsGeneralDto goodsGeneralDto = new GoodsGeneralDto();
        BeanUtils.copyProperties(goodsGeneral, goodsGeneralDto);

        return goodsGeneralDto;
    }

    /**
     * 分页+关键字查询大类信息
     *
     * @param limitParam 分页查询大类信息的参数
     * @return 分页查询返回给前端的数据
     */
    @Override
    public PageGoodsGeneralDto pageFind(ListGoodsGeneralParam limitParam) {

        QueryWrapper<GoodsGeneral> queryWrapper = new QueryWrapper<>();
        //0表示未逻辑删除，才可以查询得到
        queryWrapper.eq("goods_general_status", 0);
        if (null != limitParam.getKeyword()) {
            //模糊查询的条件应该是一个组合条件 即sql要加括号！
            queryWrapper.and(qw -> {
                qw.like("goods_general_name", limitParam.getKeyword());
            });
        }
        //加个按照主键降序的条件
        queryWrapper.orderByDesc("goods_general_id");
        IPage<GoodsGeneral> iPage = page(new Page<>(limitParam.getIndexPage(), limitParam.getPageSize()), queryWrapper);
        PageGoodsGeneralDto pageGoodsGeneralDto = new PageGoodsGeneralDto();
        //塞入当前页
        pageGoodsGeneralDto.setIndexPage(iPage.getCurrent());
        //塞入总条数
        pageGoodsGeneralDto.setTotal(iPage.getTotal());
        //塞入数据
        pageGoodsGeneralDto.setGeneralDtos(WoniuBeanUtils.copyList(iPage.getRecords(), GoodsGeneralDto.class));
        return pageGoodsGeneralDto;
    }


    /**
     * 修改大类信息
     *
     * @param updateGoodsGeneralParam 前端大类修改后的信息
     * @return 返回给前端修改后的大类信息
     */
    @Override
    public UpdateGoodsGeneralDto updateGeneralInfo(UpdateGoodsGeneralParam updateGoodsGeneralParam) {
        //先根据id查询 返回大类对象
        GoodsGeneral generalDB = getById(updateGoodsGeneralParam.getGoodsGeneralId());
        //在判断该对象是否满足修改要求
        if (generalDB.getGoodsGeneralStatus() == 1)
//            throw new GoodsGeneralException("该大类信息已删除", 1051);
            throw new GoodsGeneralException(CodeEnum.GENERAL_INFO_NULL.getMessage(), CodeEnum.GENERAL_INFO_NULL.getCode());
        BeanUtils.copyProperties(updateGoodsGeneralParam, generalDB);
        if (updateById(generalDB)) {

            return WoniuBeanUtils.copyObject(generalDB, UpdateGoodsGeneralDto.class);
        }
//        throw new GoodsGeneralException("修改失败", 1025);
        throw new GoodsGeneralException(CodeEnum.GENERAL_UPDATE_FAIL.getMessage(), CodeEnum.GENERAL_UPDATE_FAIL.getCode());

    }


    /**
     * 批量删除大类信息（逻辑删除）
     *
     * @param deleteGeneralParam
     * @return 删除成功就返回true
     */
    @Override
    public Boolean deleteGoodsGeneral(DeleteGeneralParam deleteGeneralParam) {

        List<GoodsGeneral> generalList = list(new QueryWrapper<GoodsGeneral>().in("goods_general_id", deleteGeneralParam.getGoodsGeneralIds()));
//        if (null == generalList || generalList.size() <= 0) throw new GoodsGeneralException("大类信息不存在", 1051);
        if (null == generalList || generalList.size() <= 0)
            throw new GoodsGeneralException(CodeEnum.GENERAL_INFO_NULL.getMessage(), CodeEnum.GENERAL_INFO_NULL.getCode());
        //过滤掉不符合删除条件的数据
        List<GoodsGeneral> collect = generalList.stream().filter(general -> {
            Boolean flag = !(general.getGoodsGeneralStatus() == 1);
            //逻辑删除
            if (flag) general.setGoodsGeneralStatus(1);
            return flag;
        }).collect(Collectors.toList());//将符合删除条件的数据添加到新集合内
//        if (collect.size() != generalList.size()) throw new GoodsGeneralException("有大类不满足删除条件", 1011);
        if (collect.size() != generalList.size())
            throw new GoodsGeneralException(CodeEnum.GENERAL_DELETE_NOWAY.getMessage(), CodeEnum.GENERAL_DELETE_NOWAY.getCode());



        return updateBatchById(collect);
    }


    @Override
    public List<GoodsGeneralDto> listGeneralDtos(Integer generalId) {

        List<GoodsBrandGeneral> list = service.list(new QueryWrapper<GoodsBrandGeneral>().eq("general_id", generalId));
        if (null == list || list.size() == 0) {
            return null;
        }

        List<Integer> collect = list.stream().map(
                id -> id.getGeneralId()
        ).collect(Collectors.toList());

        QueryWrapper<GoodsGeneral> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_general_status", 0).in("goods_general_name", collect);
        List<GoodsGeneral> generalList = list(queryWrapper);
        List<GoodsGeneralDto> dtos = WoniuBeanUtils.copyList(generalList, GoodsGeneralDto.class);
        return dtos;

    }


}
